package ziye.eventbustest;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import org.greenrobot.eventbus.EventBus;

import frag.BlankFragment;
import msg.MessageForFrag;

public class ActivityForFrag extends AppCompatActivity {

    private FrameLayout fm;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_for_frag);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageForFrag("来自activity的消息,你收到了么"));
            }
        });
    }

    private void initData() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.fram, new BlankFragment());
        transaction.commit();
//
//        EventBus.getDefault().register(this);
    }

    private void initView() {
        fm = (FrameLayout) findViewById(R.id.fram);
        btn = (Button) findViewById(R.id.btn_frag);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
    }
}
